package com.wicture.erp4android.ui.view;



import com.google.gson.Gson;
import com.libs.zxing.CaptureActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.wicture.erp4android.R;
import com.wicture.erp4android.bean.Order;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.AvoidXfermode.Mode;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


/**
 * 复核区界面，即分拣区
 * @author Administrator
 *
 */
public class SortingPage implements OnClickListener {
	
	public static final int UNBINDTRANCEBOX = 0;
	public static final int COMITSENDINGINFO = 1;
	public static final int COMITERRORBOX = 2;
	private Activity mActivity;
	public View mRootView;

	private Button btn_tranceBox;
	public String tranceBoxCode;
	public String sendBoxCode;
	
	private EditText et_tranceBoxcode;

private boolean isCorrect = false;
	private Button btn_cancleBox;
	private Button btn_confirmBox;
	
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
		
			case 1:
				//发货箱可用,提交数据
				moreDialog();
				
				break;
			case 2:
				//发货箱不能用
				Toast.makeText(mActivity, "发货箱未解绑或不可用", Toast.LENGTH_SHORT).show();
				//清空输入框，发货箱对象置空
				et_sendBoxcode.setText("");
				sendBoxCode = null;
				break;
			default:
				break;
			}
		}

	};
	private ProgressBar loading;
	private ListView lv_detail;
	private EditText et_sendBoxcode;
	
	private Button btn_confirmSendBox;
	private Button btn_sendBox;
	private Button btn_cancleSendBox;

	public SortingPage(Activity context) {

		mActivity = context;
		initView();

	}

	/**
	 * 初始化布局,设置点击事件
	 */
	private void initView() {
		mRootView = View.inflate(mActivity, R.layout.sorting_layout, null);
		if (mRootView != null) {
			
			btn_tranceBox = (Button) mRootView.findViewById(R.id.btn_tranceBox);
			
			et_tranceBoxcode = (EditText) mRootView.findViewById(R.id.et_tranceBoxcode);
			
			et_sendBoxcode = (EditText) mRootView.findViewById(R.id.et_sendBoxcode);
			btn_confirmSendBox = (Button) mRootView.findViewById(R.id.btn_confirmSendBox);
			btn_confirmBox = (Button) mRootView
					.findViewById(R.id.btn_confirmBox);
			btn_cancleBox = (Button) mRootView.findViewById(R.id.btn_cancleBox);
			loading = (ProgressBar) mRootView.findViewById(R.id.pb_loading);
			btn_cancleSendBox = (Button) mRootView.findViewById(R.id.btn_cancleSendBox);
			btn_sendBox = (Button) mRootView.findViewById(R.id.btn_sendBox);
			btn_sendBox.setOnClickListener(this);
			btn_confirmSendBox.setOnClickListener(this);
			btn_cancleSendBox.setOnClickListener(this);
			btn_tranceBox.setOnClickListener(this);
			btn_confirmBox.setOnClickListener(this);
			btn_cancleBox.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		
		case R.id.btn_tranceBox:
			Intent intent1 = new Intent(mActivity,
					CaptureActivity.class);
			mActivity.startActivityForResult(intent1, 0);
			break;
		case R.id.btn_sendBox:
			Intent intent2 = new Intent(mActivity, CaptureActivity.class);
			mActivity.startActivityForResult(intent2, 1);
			break;
		
		case R.id.btn_confirmBox:
			//确认中转箱，解除绑定
			//手动输入的时候读取文本框内条码
			if(tranceBoxCode==null){
				
				tranceBoxCode = et_tranceBoxcode.getText().toString();
			}
			checkOrder(tranceBoxCode);
			break;
		case R.id.btn_cancleBox:
			// 将订单对象置空
			Toast.makeText(mActivity, "取消了", Toast.LENGTH_SHORT).show();

			et_tranceBoxcode.setText("");
			tranceBoxCode = null;
			break;
		case R.id.btn_confirmSendBox:
			sendBoxCode = et_sendBoxcode.getText().toString();
			//确认发货箱，提交数据
			//检查发货箱是否可用
			System.out.println("确认发货箱");
			checkSendBox();
		case R.id.btn_cancleSendBox:	
				et_sendBoxcode.setText("");
				sendBoxCode = null;
			break;
		default:
			break;
		}

	}

	/**
	 * 设置文本框内条码数据，根据条码种类给不同文本框设置
	 * 
	 * @param categary
	 * @param codeString
	 */
	public void setString(int requestCode, String codeString) {
		switch (requestCode) {

		case 0:
			tranceBoxCode = codeString;
			et_tranceBoxcode.setText(tranceBoxCode);
			break;
		case 1:
			if(TextUtils.isEmpty(sendBoxCode)){
				sendBoxCode = codeString;
			}else{
				sendBoxCode += "_";
				sendBoxCode += codeString;
			}
			
			et_sendBoxcode.setText(codeString);
			break;

		default:
			break;
		}
	}

	/**
	 * 请求网络，查询订单详情,扫码结束和手动输入确认后调用
	 * 
	 * @param code
	 * @return
	 */
	public void checkOrder(String code) {
		boolean flag = false;
		if (code.length() > 8) {
			loading.setVisibility(View.VISIBLE);
			HttpUtils http = new HttpUtils();
			 http = new HttpUtils(2000);

			http.send(HttpMethod.GET, "http://www.baidu.com",
					new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException e, String arg1) {
							// TODO Auto-generated method stub
							e.printStackTrace();
							loading.setVisibility(View.GONE);
							//测试代码
							processData();
						}

						@Override
						public void onSuccess(ResponseInfo<String> response) {
							// 获取服务器响应的数据
							loading.setVisibility(View.GONE);
							String result = response.result;
							System.out.println("响应码"+result);
//							Toast.makeText(mActivity, "响应"+result, Toast.LENGTH_SHORT).show();
							processData();
							

						}
					});
			System.out.println("参数" + code);
			http = null;
		} else {
			Toast.makeText(mActivity, "订单号有误", Toast.LENGTH_SHORT).show();
		}
		
	}

	/**
	 * 弹出对话框，显示订单详情
	 */
	protected void showDialog() {
//		WindowManager wm = mActivity.getWindowManager();
//		Display display = wm.getDefaultDisplay();
//		LayoutParams params = mActivity.getWindow().getAttributes();
//		params.height = (int) (display.getHeight() * 0.8);
//		params.width = (int) (display.getWidth() * 0.9);
		AlertDialog alertDialog;
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle("订单详情");
		View dialogView = View.inflate(mActivity, R.layout.dialog_orderdetail,
				null);
		
		builder.setView(dialogView);
		builder.setPositiveButton("正确", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				//订单有误，提交错误信息
				tranceBoxCode = et_tranceBoxcode.getText().toString();
				comitData(UNBINDTRANCEBOX);
			}
		});
		builder.setNegativeButton("有误", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 先清空输入框和中转箱信息，再提交问题订单
				et_tranceBoxcode.setText("");
				tranceBoxCode = null;
				comitData(COMITERRORBOX);
			}
		});
		
		
		
		builder.setCancelable(false);
		 alertDialog = builder.create();
		
		builder.show();
//		alertDialog.setContentView(dialogView, params);
//		alertDialog.show();

	}

	/**
	 * 解析订单详情
	 */
	private void processData() {
		//解析json数据封装对象
		Gson gson = new Gson();
		
		//展示对话框,核对信息
		
			
			showDialog();
		
		
	}
	
	
	//提交绑定的周转箱以及操作员的编号,以及发货箱
	public void comitData(int categary){
		//获取操作员工编号
				SharedPreferences sp = mActivity.getSharedPreferences("config",mActivity.MODE_PRIVATE);
				String userName = sp.getString("user", null);
		switch(categary){
		case UNBINDTRANCEBOX:
			//解除绑定周转箱
			HttpUtils http = new HttpUtils();
			http.send(HttpMethod.GET, "http://www.baidu.com", new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					Toast.makeText(mActivity, "服务器无响应，请重试连接", Toast.LENGTH_SHORT).show();
					
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					
					Toast.makeText(mActivity, "解绑周转箱成功", Toast.LENGTH_SHORT).show();
				}
			});
			http = null;
			break;
		case COMITSENDINGINFO:
			//提交发货信息
			HttpUtils httpSendBox = new HttpUtils();
			httpSendBox.send(HttpMethod.GET, "http://www.baidu.com", new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			httpSendBox = null;
			break;
		case COMITERRORBOX:
			//提交错误订单信息
			HttpUtils httpErrorBox = new HttpUtils();
			httpErrorBox.send(HttpMethod.GET, "http://www.baidu.com/", new RequestCallBack<String>() {

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(ResponseInfo<String> arg0) {
					// TODO Auto-generated method stub
					
				}
			});
			break;
			default:
				break;
		}
		
	}
	
	/**
	 * 检查发货箱是否可用
	 */
	public void checkSendBox(){
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.GET, "http://www.baidu.com", new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException e, String arg1) {
				//测试代码
				Toast.makeText(mActivity, "服务器无响应，请重试", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onSuccess(ResponseInfo<String> res) {
				// TODO Auto-generated method stub
				
				if(res.result.equals("")){
					handler.sendEmptyMessage(1);
				}else{
					handler.sendEmptyMessage(2);
				}
				//测试代码
				handler.sendEmptyMessage(2);
			}
		});
		
		http = null;
	}
	
	/**
	 *  弹出对话框，确定要不要增加一个发货箱
	 */
	private void moreDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle("添加发货箱");
		builder.setPositiveButton("继续", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(mActivity,CaptureActivity.class);
				mActivity.startActivityForResult(intent, 1);
				
			}
		});
		builder.setNegativeButton("完成", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 提交数据
				comitData(COMITSENDINGINFO);
				//清除数据
				et_tranceBoxcode.setText("");
				et_sendBoxcode.setText("");
				tranceBoxCode = null;
				sendBoxCode = null;
			}
		});
		builder.create();
		builder.show();
	}
	
//}
//(HttpMethod.GET, "http://www.baidu.com", new RequestCallBack<String>() {
//
//			@Override
//			public void onFailure(HttpException arg0, String arg1) {
//				// TODO Auto-generated method stub
//				
//			}
//
//			@Override
//			public void onSuccess(ResponseInfo<String> arg0) {
//				//检验成功就绑定发货箱
//				comitData(COMITSENDINGINFO);
//			}
//		});
//	}
	
	
	
}
